<template>
	<view class="container">
		<view class="yy-search-top">
			<view class="yy-search-topipt">
				<view class="yuyue-head-iptseaicon">
					<image src="../../static/icon_search.png" mode="widthFix"></image>
				</view>
				<input type="text" v-model="keyword" @input="searchEvent" confirm-type="search" placeholder-style="color:#7D8799" placeholder="输入关键词搜索" />
				<view class="yuyue-head-iptdelicon">
					<image src="../../static/ic_clear.png" mode="widthFix" @click="clearcon"></image>
				</view>
			</view>
			<view class="yy-search-topcan" @click="pverpage">取消</view>
		</view>
		<view class="yy-search-conhisrecord" v-if="search">
			<view class="yy-search-conhead">
				<view class="yy-search-conhead-l">历史搜索</view>
				<view class="yy-search-conhead-r" @click="delsearch">
					<image src="../../static/ic_trash.png" mode="widthFix"></image>
				</view>
			</view>
			<view class="yy-search-consub" v-if="historylist != '' ">
				<view class="yy-search-item" v-for="(item,index) in historylist" :key="index" @click="changehis(item)"><text>{{item}}</text></view>
			</view>
		</view>
		<view class="yy-search-conseasub" v-if="searchList.length>0">
			<view class="yy-search-conseasub-item" v-for="(item,index) in searchList" @click="jumpmz(item.id)" :key="index">{{item.name}}</view>
		</view>
		<view class="yy-search-conseasubnull" v-else>
			<view class="yy-search-nullimg">
				<image src="../../static/img_blank_search.png" mode="widthFix"></image>
			</view>
			<view class="yy-search-nulltext">
				<text>没搜索到相关内容，换个词试试吧</text>
			</view>
		</view>
	</view>
</template>

<script>
	import app from '../../App.vue';
	export default{
		data(){
			return{
				search:true,
				keyword:'',
				searchList:[],
				historylist:[]
			}
		},
		onLoad() {
			this.getSearchHistory()
		},
		onShow() {
			
		},
		methods: {
			getSearchHistory(){      //所搜历史
				let that =this,
				data= {
					limit:6
				}
				app.post('api/keyword_log/getKeyword',data).then(res=>{
					if(res.code == 1){
						let list = res.data
						list.forEach((res,index)=>{
							if(res == ""){
								console.log(res)
								list.splice(index,1)
							}else if(res == null){
								list.splice(index,1)
							}
						})
						console.log(list)
						that.historylist = list
					}
					
				})
			},
			delsearch(){
				let that =this
				app.post('api/keyword_log/delKeyword',{}).then(res=>{
					if(res.code == 1){
						this.getSearchHistory()
					}else{
						
					}
					
				})
			},
			clearcon(){       //清空搜索框
				this.keyword = ''
			},
			pverpage(){       //取消搜索
				uni.navigateBack({
					delta:0
				})
			},
			searchEvent(){      //搜索
				let that = this,
				data = {
					keyword:that.keyword,
					limit:''
				}
				that.search = false
				uni.showLoading({
					title:'',
					success() {
						uni.hideLoading({
							success:res=>{
								app.post('api/department/lis',data).then(res=>{
									if(res.code == 1){
										that.searchList = res.data
									}
								})
							}
						})
					}
				})
			},
			jumpmz(id){        //科室跳转
				uni.showLoading({
					title:'加载中',
					success() {
						setTimeout(function(){
							uni.hideLoading({
								success:res=>{
									uni.navigateTo({
										url:'/pages/yuyue/menzhen?id='+id
									})
								}
							})
						},1300)
					}
				})
			},
			changehis(con){
				let that = this
				that.keyword = con
				this.searchEvent()
			}
		}
	}
</script>

<style>
	@import url("../../common/yuyuesearch");
</style>
